	package browser.service;

	import browser.dao.UserInfo;
	import java.io.*;
	import java.sql.*;
	import javax.servlet.*;
	import javax.servlet.http.*;

	/**
	 * Servlet that will display table metadata (column names, data type, data length)
	 *
	 */
	public class ProcessTables extends HttpServlet {

		public static Connection conn;
		public static Statement stmt;

		public void doGet(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {

			String[] uid = request.getParameterValues("uid");
			String[] uacc = request.getParameterValues("uAccess");
			String[] tname = request.getParameterValues("tName");
			String conf = request.getParameter("confirm");

			try {
				Class.forName("oracle.jdbc.driver.OracleDriver");
			} catch (ClassNotFoundException e) {
				// out.println("Error loading the Driver:"+e.getMessage());
				return;
			}

			Connection conn = null;
			UserInfo user = MyUtilities.getUserInfo(uid[0]);

			if ((user.getUaccess() == null)
					|| (!uacc[0].equals(user.getUaccess()))) {
				// out.print("Please login first");
				return;
			}
			try {
				conn = DriverManager.getConnection("jdbc:oracle:thin:"
						+ user.getUid() + "/" + user.getPassword()
						+ "@oraserver.cs.luc.edu:1521:cs");
			} catch (SQLException e1) {
				// out.println("Error connecting to Oracle:"+e1.getMessage());
				return;
			}
			if (conn == null) {
				// out.println("Null Connection");
				return;
			}

			Statement stmt = null;
			try {
				stmt = conn.createStatement();
			} catch (SQLException e) {
				// out.println("createStatement " + e.getMessage());
				try {
					conn.close();
				} catch (SQLException e2) {
				}
				;
				return;
			}

			if (conf != null && conf.equals("Y")) {

				try {
					stmt.executeUpdate("drop table " + tname[0]
							+ " cascade constraints");
					stmt.close();
					conn.close();

				} catch (SQLException e) {
					e.printStackTrace();
					// out.println(e.getMessage());
				}

				// out.close();

				// response.sendRedirect("/DatabaseBrowser/ProcessTables");
				return;
			}

		}

		public void doPost(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();

			// Check if session is expired
			if (request.getSession(false) == null
					|| (Boolean) request.getSession(false).getAttribute(
							"expired") == true) {
				out.println("Session expired");
				return;
			}

			String[] uid = request.getParameterValues("uid");
			String[] uacc = request.getParameterValues("uAccess");
			String[] tname = request.getParameterValues("tName");

			try {
				Class.forName("oracle.jdbc.driver.OracleDriver");
			} catch (ClassNotFoundException e) {
				out.println("Error loading the Driver:" + e.getMessage());
				return;
			}

			Connection conn = null;
			UserInfo user = MyUtilities.getUserInfo(uid[0]);

			if ((user.getUaccess() == null)
					|| (!uacc[0].equals(user.getUaccess()))) {
				out.print("Please login first");
				return;
			}
			try {
				conn = DriverManager.getConnection("jdbc:oracle:thin:"
						+ user.getUid() + "/" + user.getPassword()
						+ "@oraserver.cs.luc.edu:1521:cs");
			} catch (SQLException e1) {
				out.println("Error connecting to Oracle:" + e1.getMessage());
				return;
			}
			if (conn == null) {
				out.println("Null Connection");
				return;
			}

			Statement stmt = null;
			try {
				stmt = conn.createStatement();
			} catch (SQLException e) {
				out.println("createStatement " + e.getMessage());
				try {
					conn.close();
				} catch (SQLException e2) {
				}
				;
				return;
			}

			out.println("<html>");
			out.println("  <head><title>" + tname[0]);
			out
					.println("  </title><script type=\"text/javascript\" src=\"dojo.js\"></script>");
			out
					.println("<script type=\"text/javascript\" src=\"deltable.js\"></script><link type=\"text/css\" rel=\"stylesheet\" href=\"main.css\"/></head>");
			out.println("  <body>");
			out.println("    <p>");
			out.println("    <center>");
			out.println("      <h2>" + tname[0] + "</h2>");

			ResultSet rset = null;
			String query = "select * from " + tname[0];
			// String query =
			// "select column_name,data_type,data_precision,nullable from user_tab_columns"
			// +
			// " where table_name='"+tname[0]+"'";
			try {
				rset = stmt.executeQuery(query);
			} catch (SQLException e) {
				out.println("executeQuery " + e.getMessage());
				return;
			}

			out
					.println("<form action=\"/DatabaseBrowser/QueryTable\" method=\"post\">");

			try {
				out.println("<table border=2 id=\"table\">");
				out.println("<tr>");
				out.println("<th>CNAME</th>");
				out.println("<th>CTYPE</th>");
				out.println("<th>LENGTH</th>");
				out.println("<th>NULL?</th>");
				out.println("<th>QUERY?</th>");
				out.println("</tr>");

				ResultSetMetaData meta = rset.getMetaData();
				for (int i = 1; i <= meta.getColumnCount(); i++) {
					out.println("<tr>");
					out.println("<td>" + meta.getColumnLabel(i) + "</td>");
					out.println("<td>" + meta.getColumnTypeName(i) + "</td>");

					String temps = "";
					if (meta.getColumnTypeName(i).equals("NUMBER")) {
						if (meta.getPrecision(i) == 0)
							temps = "";
						else if (meta.getScale(i) != 0)
							temps = "(" + meta.getPrecision(i) + ","
									+ meta.getScale(i) + ")";
						else
							temps = meta.getPrecision(i) + "";
					} else if (meta.getColumnTypeName(i).equals("DATE")) {
						temps = "";
					} else {
						temps = meta.getPrecision(i) + "";
					}
					out.println("<td>" + temps + "</td>");

					out.println("<td>" + (meta.isNullable(i) == 0 ? "N" : "Y")
							+ "</td>");
					out
							.println("<td><input type=\"checkbox\" name=\"qCols\" value=\""
									+ meta.getColumnName(i) + "\" /></TD>");
					out.println("</tr>");
				}
				out.println("</table>");

				out.println("<input type=\"hidden\" name=\"uid\" value=\""
						+ uid[0] + "\">");
				out.println("<input type=\"hidden\" name=\"uAccess\" value=\""
						+ uacc[0] + "\">");
				out.println("<input type=\"hidden\" name=\"tName\" value=\""
						+ tname[0] + "\">");
				out.println("<input type=\"submit\" value=\"Query\">");
				out.println("</form>");
				out.println("</br>");
				out.println("<input id=\"deltable\" type=\"button\" value=\"Delete Table\"/>");
				/*
				 * while(rset.next()){ out.println("<tr>");
				 * out.println("<td>"+rset.getString("column_name")+"</td>");
				 * out.println("<td>"+rset.getString("data_type")+"</td>");
				 * out.println("<td>"+rset.getString("data_precision")+"</td>");
				 * out.println("<td>"+rset.getString("nullable")+"</td>");
				 * out.println("</tr>"); }
				 */

			} catch (SQLException e) {
				e.printStackTrace();
			}

			out.println("    </center>");
			out.println("    </p>");
			out.println("  </body>");
			out.println("</html>");

			try {
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			out.close();
		}
	}
